package com.youlian.utils.bitmap;


import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Pattern;

import android.text.TextUtils;

import com.youlian.utils.FileHelper;



/**
 *
 * @author simon
 *
 */
public class URLHelper {



	public String originalURL;

	public String filename;

	public String suffix;

	public int type;
	/**
	 *
	 * @param urlString
	 * @param type
	 */
	public URLHelper(String urlString,int type){

		if(isURL(urlString)){

			int beginIndex = urlString.lastIndexOf("/") + 1;

			filename = urlString.substring(beginIndex).trim();

			originalURL = urlString;

			suffix = FileHelper.getSuffix(filename);

		}
		this.type = type;
	}

	public URLHelper(String urlString){
		this(urlString,0);
	}

	/**
	 *
	 * @param fileUrl
	 * @return
	 */
	public static String getDownfileName(String fileUrl){
		String s1 = fileUrl.substring(fileUrl.lastIndexOf('/')+1,fileUrl.length());
		String returnUrl = s1.substring(0, s1.indexOf('.'));
		return returnUrl;
	}


	public String getURL(){
	return  originalURL; //首图先不用图片压缩
//		return ImageUrlFactory.notify(originalURL, type);
	}

	private static final Pattern PATTERN = Pattern.compile("(http://|https://){1}[\\w\\.\\-/:]+");
	public static boolean isURL(String url){
		if(!TextUtils.isEmpty(url) &&  PATTERN.matcher(url).find()){
			return true;
		}else{
			return false;
		}
	}

	public static String getHost(String url) throws MalformedURLException{
		return new URL(url).getHost();
	}
}
